package com.asion.qkan.ui.fragment;

import android.animation.ObjectAnimator;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.adapters.HomeRecyAdapter;
import com.asion.qkan.entry.HomeBean;
import com.asion.qkan.ui.contract.HomeContract;
import com.asion.qkan.ui.model.HomeModel;
import com.asion.qkan.ui.presenter.HomePresenter;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;

/**
 * Created by asion on 16-10-12.
 */

public class HomeFragment extends BaseFragment<HomePresenter, HomeModel> implements HomeContract.View, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.monday_recycler)
    RecyclerView mondayRecycler;
    @BindView(R.id.home_scroll)
    ScrollView homeScroll;
    @BindView(R.id.home_linear)
    LinearLayout homeLinear;
    @BindView(R.id.home_fab)
    FloatingActionButton homeFab;
    @BindView(R.id.fab_textview)
    TextView fabTextview;
    @BindView(R.id.swipFresh)
    SwipeRefreshLayout swipFresh;
    public static final String category = "comic_lists";

    private String[] url = {"1", "0", "1475510400", "1475596800", "1475683200", "1475769600", "1475856000"};
    private int[] imgId = {R.drawable.seven, R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five, R.drawable.six};

    //为其设置适配器
    private HomeRecyAdapter mondayRecyAdapter;
    private int temp;
    @Override
    protected int getLayoutResource() {
        return R.layout.home_fragment;
    }


    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
        mPresenter.setHomeDatas(category,url[getDiffId()]);//默认显示当天的数据
    }

    protected void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mondayRecycler.setLayoutManager(linearLayoutManager);
        mondayRecyAdapter = new HomeRecyAdapter(getContext());//数据源在这个时候是空的
        mondayRecycler.setAdapter(mondayRecyAdapter);


        addView();
        homeFab.setOnClickListener(this);
        fabTextview.setOnClickListener(this);

        swipFresh.setOnRefreshListener(this);
        swipFresh.setSize(SwipeRefreshLayout.LARGE);
        swipFresh.setColorSchemeColors(getResources().getColor(R.color.colorAccent));
    }

    ////添加图片
    private void addView() {
        //给ScrollView添加内容
        for (int i = 0; i <= 6; i++) {
            ImageButton imgBtn = new ImageButton(getContext());
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 80);
            imgBtn.setLayoutParams(layoutParams);
            imgBtn.setBackgroundResource(R.drawable.ww);
            imgBtn.setScaleType(ImageView.ScaleType.CENTER_CROP);
            int img_id = imgId[i];
            imgBtn.setImageResource(img_id);
            String string = url[i];
            int j = i;
            imgBtn.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
                @Override
                public void onClick(View view) {
                    changeContent(string);
                    homeFab.setImageResource(img_id);
                    temp = j;
                }
            });
            homeLinear.addView(imgBtn);
        }
    }

    //改变页面
    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private void changeContent(String id) {
        startProgressDialog();
        homeScroll.setVisibility(View.INVISIBLE);
        closeMenu(homeFab);
        mondayRecyAdapter.clean();
        mPresenter.setHomeDatas(category,id);
    }

    @Override
    public void showDataSuccess(HomeBean homeBean) {
        mondayRecyAdapter.addAll(homeBean.getData().getComics());
        swipFresh.setRefreshing(false);
        stopProgressDialog();
    }

    @Override
    public void showDataError(Throwable throwable) {
        throwable.printStackTrace();
    }

    //获取当天是哪天
    private int getDiffId() {
        //获取到当天的
        Date now = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        Log.e("s", w + "");
        if (w < 0) {
            w = 0;
        }
        //给fab设置默认的图片
        homeFab.setImageResource(imgId[w]);
        temp = w;
        return w;
    }


    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_fab:
                int height = view.getHeight();
                if (homeScroll.getVisibility() == View.VISIBLE) {
                    closeMenu(view);
                    homeScroll.setVisibility(View.INVISIBLE);
                } else {
                    openMenu(view);
                    ViewCompat.setTranslationY(homeScroll, -height + 20);
                    homeScroll.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.fab_textview:
                homeLinear.setVisibility(View.INVISIBLE);
                closeMenu(homeFab);
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private void openMenu(View view) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "rotation", 0, -155, -135);
        objectAnimator.setDuration(500);
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        objectAnimator.start();
        fabTextview.setVisibility(View.VISIBLE);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 0.75f);
        alphaAnimation.setDuration(500);
        alphaAnimation.setFillAfter(true);
        fabTextview.startAnimation(alphaAnimation);
        fabTextview.setClickable(true);
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private void closeMenu(View view) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "rotation", -135, 20, 0);
        objectAnimator.setDuration(500);
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        objectAnimator.start();
        fabTextview.setVisibility(View.GONE);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.75f, 0);
        alphaAnimation.setDuration(500);
        alphaAnimation.setFillAfter(true);
        fabTextview.startAnimation(alphaAnimation);
        fabTextview.setClickable(false);
    }

    @Override
    public void onRefresh() {
        mondayRecyAdapter.clean();
        mPresenter.setHomeDatas(category,url[temp]);
    }
}
